package com.sunyifeng.ch32;

public class ServiceLocator {
    private static Cache cache;

    static {
        cache = new Cache();
    }

    public static Service getService(String jndiName){

        Service service = cache.getService(jndiName);

        if(service != null){
            return service;
        }

        //从上下问题获取服务
        InitialContext context = new InitialContext();
        service = (Service)context.lookup(jndiName);
        //服务放入缓存
        cache.addService(service);
        return service;
    }
}
